// 导入express
const express = require('express')

// 创建app
const app = express()

app.all('*', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
  res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
  res.header("X-Powered-By",' 3.2.1')
  if(req.method=="OPTIONS") res.send(200);/*让options请求快速返回*/
  else  next();
})

// 处理请求
app.get('/api/users',(req,res) => {
  res.json({
    status:0,
    message:[
      {id:1001,name:'张无忌',address:'光明顶'},
      {id:1002,name:'乔峰',address:'大辽皇宫'},
      {id:1003,name:'令狐冲',address:'华山'}
    ]
  })
})

// 开启监听
app.listen(3000,'127.0.0.1',err => {
  if (err) {
    console.log(err)
  }

  console.log('start OK')
})
